package com.ws.demo1.demos.ConcurrencyBasics;

import java.util.concurrent.atomic.AtomicInteger;

//使用原子类操作解决线程安全问题
public class AccessCounterByCAS extends Thread {

    /**
     * 访问接口的次数
     */
    AtomicInteger accessCount = new AtomicInteger(0);
    public void access(){
        accessCount.incrementAndGet();
        System.out.println("result is"+accessCount.get());
    }

    @Override
    public void run() {
        access();
    }

    public static void main(String[] args) {
        AccessCounterByCAS accountCount = new AccessCounterByCAS();
        new Thread(accountCount,"A").start();
        new Thread(accountCount,"B").start();
        new Thread(accountCount,"C").start();
        new Thread(accountCount,"D").start();
        new Thread(accountCount,"E").start();
        new Thread(accountCount,"F").start();
    }
    /**
     * result is1
     * result is2
     * result is3
     * result is4
     * result is5
     * result is6
     */
}
